package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.car.actionform.LicenseplateForm;
import com.tianqu.tms.www.car.controller.LicenseplateModifyController;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class LicenseplateModifyAction extends TmsAction{
	private Logger logger = Logger.getLogger(LicenseplateModifyAction.class);
	private final static String DETAIL = "detail";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
		LicenseplateForm licenseplateForm = (LicenseplateForm) uc.getInputForm();
		
		if(!StringUtil.isNullOrBlank(licenseplateForm.getFuncId())){
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			return mapping.findForward(TmsConstants.SUCCESS);
		} 
		
		
		if(StringUtil.isNullOrBlank(licenseplateForm.getId())){
			if (StringUtil.isNullOrBlank(licenseplateForm.getVin()) ) {
				setErrorsToRequest(request, TmsConstants.U0040, null);
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}
			
			if (!StringUtil.isNullOrBlank(licenseplateForm.getVin()) && licenseplateForm.getVin().length() !=6) {
				setErrorsToRequest(request, TmsConstants.U8010, null); //
				request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
				return mapping.findForward(TmsConstants.SUCCESS);
			}
		}
		//
		LicenseplateModifyController controller = new LicenseplateModifyController();
		controller.execute(uc);
		
		//
		
		viewhelper = (CarStockModifyViewhelper) uc.getAttribute(TmsConstants.VIEWHELP);
		if (viewhelper.getConfirmFlag() == 1) {
			setErrorsToRequest(request, TmsConstants.U0039, null);
		}else if(viewhelper.getConfirmFlag() == 2){
			 request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			 return mapping.findForward(DETAIL);
		}
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
